package com.jietuo.class02;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2024/3/3
 * @description: 8，求数组arr[L,R]中的最大值，怎么用递归方法实现
 */
public class Code08_Recursion {

    public static void main(String[] args) {

        int[] arr = new int[]{3, 6, 8, 1, 8, 12, 23,10};
        int min = getMin(arr, 0, arr.length - 1);
        System.out.println(min);
    }

    public static int getMin(int[] arr, int left, int right) {
        if (arr.length <= 0) {
            throw new RuntimeException("数组大小问题");
        }
        if (left == right) {
            return arr[left];
        }
        // 将left - right 上一分为2
        int mid = (left + right) / 2;
        int leftMin = getMin(arr, left, mid);
        int rightMin = getMin(arr, mid + 1, right);
        return Math.min(leftMin, rightMin);
    }
}
